<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelReportTransfer extends JModel
{
	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct(){
		parent::__construct();
	}

	/**
	 * Method to get today's for transaction
	 *
	 * @return array
	 * @since 1.0
	 */
	function getTodays($day)
	{
		//Initialise variables
		$user			=& JFactory::getUser();
		$ownuser	=& Ownservice::getUser();

		$where		= array();

		if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
		{	
			$where[] = 't.branch_id ='.$ownuser->get('branch_id');
		}

		$where[]		= '(t.requested_date >= '.$this->_db->Quote($day.' 00:00:00').' AND t.requested_date <= '.$this->_db->Quote($day.' 23:59:59').')';

		$where[]		= 't.state < 8';

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$query		= ' SELECT t.session, t.requested_date, t.amount, t.total, t.total_th, t.extratotal, c.iscore, c.code '
							.' FROM #__ownservice_transactions AS t '
							.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id=t.rate_id '
							.' LEFT JOIN #__ownservice_currencies AS c ON c.id=r.currency_id '
							.$where
							/*.' GROUP BY t.session ' */
							.' ORDER BY t.session'
						   ;

		$this->_db->setQuery($query);
		//echo $this->_db->_sql;
		$rows = $this->_db->loadObjectList();

		$datas = array();
		if($rows) {
			foreach($rows as $row) {
				$datas[$row->session]['requested_date'] = $row->requested_date;
				$datas[$row->session]['sumamount'] = $datas[$row->session]['sumamount'] + $row->amount;
				$datas[$row->session]['sumtotal'] = $datas[$row->session]['sumtotal'] + (($row->extratotal > 0.00)?$row->extratotal:(($row->iscore)?$row->total:$row->total_th));
			}
		}

		return $datas;
	}

	/**
	 * Method to get Month's for transaction
	 *
	 * @return array
	 * @since 1.0
	 */
	 function getMonthlyreport(){

		 //Initialise variables
		$user			=& JFactory::getUser();
		$ownuser	=& Ownservice::getUser();

		$where		= array();

		if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
		{	
			$where[] = 't.branch_id ='.$ownuser->get('branch_id');
		}

		 $unix1	= mktime(date("H"),date("i"),date("s"),date("n")-1,1,date("Y"));

		 $l_m		= date('m', $unix1);
		 $l_y		= date('Y', $unix1);

		if((int) date("d")  <= 10) {
			$where[]		= '(t.requested_date >= '.$this->_db->Quote($l_y.'-'.$l_m.'-1 00:00:00').' AND t.requested_date <= '.$this->_db->Quote(date("Y").'-'.date("m").'-'.date("d").' 23:59:59').')';
		} else {
			$where[]		= '(t.requested_date >= '.$this->_db->Quote(date("Y").'-'.date("m").'-1 00:00:00').' AND t.requested_date <= '.$this->_db->Quote(date("Y").'-'.date("m").'-'.date("d").' 23:59:59').')';
		}
		
		$where[]		= 't.state < 8';

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		 $query		= ' SELECT t.session, t.requested_date, t.amount, t.total, t.total_th, t.extratotal, c.iscore, c.code '
							.' FROM #__ownservice_transactions AS t '
							.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id=t.rate_id '
							.' LEFT JOIN #__ownservice_currencies AS c ON c.id=r.currency_id '
							.$where
							/*.' GROUP BY DATE(t.requested_date) ' */
							.' ORDER BY t.requested_date'
						   ;

		$this->_db->setQuery($query);
		//echo $this->_db->_sql;
		$rows = $this->_db->loadObjectList();

		$datas = array();
		if($rows) {
			foreach($rows as $row) {
				$day = substr($row->requested_date,0,10);
				$datas[$day]['requested_date'] = $row->requested_date;
				$datas[$day]['sumamount'] = $datas[$day]['sumamount'] + $row->amount;
				$datas[$day]['sumtotal'] = $datas[$day]['sumtotal'] + (($row->extratotal > 0.00)?$row->extratotal:(($row->iscore)?$row->total:$row->total_th));
			}
		}

		return $datas;
	 }

	function getDetailTransfer()
	{
		//Initialise variables
		$user			=& JFactory::getUser();
		$ownuser	=& Ownservice::getUser();

		//get vars
		$session			= JRequest::getVar('session', 0, 'post', 'int');
		$date				= JRequest::getVar('date', '', 'post');

		$where		= array();

		if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
		{	
			$where[] = 't.branch_id ='.$ownuser->get('branch_id');
		}
		
		$where[]		= 't.session='.$session;

		$where[]		= '(t.requested_date >= '.$this->_db->Quote($date.' 00:00:00').' AND t.requested_date <= '.$this->_db->Quote($date.' 23:59:59').')';

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$query	= '	 SELECT t.*, u.username, p.id AS memberid, a.bank_name, a.branch_city, c.iscore, c.code AS currencycode '
						.' FROM #__ownservice_transactions AS t '
						.' LEFT JOIN #__users AS u ON u.id=t.user_id '
						.' LEFT JOIN #__ownservice_users AS p ON p.user_id=u.id '
						.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
						.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id=t.rate_id '
						.' LEFT JOIN #__ownservice_currencies AS c ON c.id=r.currency_id '
						.$where
						.' GROUP BY t.id '
						.' ORDER BY t.id '
						;
		$this->_db->setQuery($query);
		//echo $this->_db->_sql;
		$rows = $this->_db->loadObjectList();

		return $rows;
	}
}